﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System.Windows;
using System.Windows.Controls;
using FinPlusInterfaces;

namespace FinPlusCompView
{
    //Note Prime use edit template columns use primative controls for data template
    public partial class FinPlusValueOnOffButton : FinPlusControl
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }
        [State(Test = true)]
        public override string TestString { get { return string.Format("{0}|{1}", IsEnabled, IsChecked); } }
        [State(Test = true)]
        public string ViewString { get { return string.Format("{0}|{1}", onOff.IsEnabled, onOff.IsChecked); } }

        public override string Id { get { return GetValue(IdProperty).ToString(); } set { } }
        public object IsChecked { get { return GetValue(IsCheckedProperty); } set { SetValue(IsCheckedProperty, value); } }
        public string Type { get { return GetValue(TypeProperty).ToString(); } set { } }
        public new bool IsEnabled { get { return (bool)GetValue(IsEnabledProperty); } set { } }
        public static readonly DependencyProperty IdProperty = DependencyProperty.Register(PodField.Id.ToString(), typeof(string), typeof(FinPlusValueOnOffButton));
        public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(BeanField.Type.ToString(), typeof(string), typeof(FinPlusValueOnOffButton));
        public new static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register(Propertys.IsEnabled.ToString(), typeof(bool), typeof(FinPlusValueOnOffButton));
        public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(Propertys.IsChecked.ToString(), typeof(object), typeof(FinPlusValueOnOffButton), new UIPropertyMetadata(Callback));

        private bool _hasSetEnable;
        private enum Propertys {IsEnabled, IsChecked }

        //construct
        public FinPlusValueOnOffButton()
        {
            InitializeComponent();
            IsValid = true;

            onOff.Cursor = ((TextBlock)this.Resources["CHit"]).Cursor;
        }

        //public
        public void SetControl()
        {
            //permission
            if (!_hasSetEnable)
            {
                onOff.IsEnabled = IsEnabled && Permissions.Get(Type, PodField.Id.ToString(), Id).CanWrite;
                _hasSetEnable = true;
            }
        }

        //event
        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs a) { ((FinPlusValueOnOffButton)d).SetControl(); }
    }
}